import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { StepMaterialModel } from '#/api/process/models';

import { getLineListApi } from '#/api/master/line';
import { getMaterialListApi } from '#/api/master/material';
import {
  useMaterialAttrOptions,
  useMaterialTypeOptions,
} from '#/api/master/models';
/**
 * 获取编辑表单的字段配置。
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'ApiSelect',
      componentProps: {
        showSearch: true,
        optionFilterProp: 'label',
        labelField: 'name',
        valueField: 'id',
        api: getMaterialListApi,
      },
      fieldName: 'materialId',
      label: '物料',
      rules: 'required',
      formItemClass: 'col-span-2 items-baseline',
    },
    {
      component: 'InputNumber',
      fieldName: 'seq',
      label: '顺序号',
      defaultValue: 0,
      rules: 'required',
      help: '只有在考虑上料顺序才需要填写',
    },
    {
      component: 'InputNumber',
      componentProps: {
        min: 0,
        precision: 0,
      },
      fieldName: 'qty',
      label: '用量',
      defaultValue: 1,
      rules: 'required',
      help: '单个的关键物料不需要修改，默认为1即可',
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        options: [
          { label: '否', value: false },
          { label: '是', value: true },
        ],
      },
      fieldName: 'isCheckSource',
      label: '校验来源',
      defaultValue: false,
      help: '上料时检查物料是否有在其他产线作为产品完工，仅用于关键物料',
      formItemClass: 'col-span-2 items-baseline',
    },
    {
      component: 'ApiSelect',
      dependencies: {
        triggerFields: ['isCheckSource'],
        trigger(values) {
          if (!values.isCheckSource) {
            values.sourceLineIds = [];
          }
        },
        disabled(values) {
          return !values.isCheckSource;
        },
      },
      componentProps: {
        mode: 'multiple',
        showSearch: true,
        optionFilterProp: 'label',
        allowClear: true,
        labelField: 'lineName',
        valueField: 'id',
        api: getLineListApi,
      },
      fieldName: 'sourceLineIds',
      label: '物料来源产线',
      formItemClass: 'col-span-2 items-baseline',
    },
  ];
}

/**
 * 获取表格列配置。
 * @param onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  onActionClick?: OnActionClickFn<StepMaterialModel>,
): VxeTableGridOptions<StepMaterialModel>['columns'] {
  return [
    { type: 'seq', width: 60 },
    { field: 'material.name', title: '物料名称', minWidth: 120 },
    { field: 'material.code', title: '物料编码', width: 220 },
    {
      field: 'material.type',
      title: '物料类型',
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: useMaterialTypeOptions(),
      },
    },
    {
      field: 'material.attr',
      title: '物料属性',
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: useMaterialAttrOptions(),
      },
    },
    { field: 'seq', title: '顺序号', width: 100 },
    { field: 'qty', title: '用量', width: 100 },
    {
      field: 'isCheckSource',
      title: '校验来源',
      width: 100,
      formatter: ({ cellValue }) => (cellValue ? '是' : '否'),
    },
    {
      cellRender: {
        attrs: {
          nameField: '',
          nameTitle: '工艺物料',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          'edit', // 默认的编辑按钮
          'delete', // 默认的删除按钮
        ],
      },
      field: 'operation',
      fixed: 'right',
      title: '操作',
      width: 120,
    },
  ];
}
